var ProOrderTemplateConfig = function (templates) {
  var self = this;
  var fields = ['material_type', 'material_name', 'material_model', 'color', 'color_description', 'provider_name',
    'quality_type', 'feed_total', 'remark'
  ];
  var fn = {
    initData: function (templates) {
      var template = templates[0];
      var config;
      if (template === undefined || SysUtil.isEmpty(template.template)) {
        config = {};
      } else {
        config = JSON.parse(template.template);
      }

      var materialTypeConfig = [];
      var materialTypeObj = {};
      for (var i = 0; i < fields.length; i++) {
        var field = fields[i];
        var obj = config[field];
        if (obj === undefined) {
          obj = {
            width: 100,
            field: field,
            align: 'center',
            hidden: false
          };
        } else {
          obj.field = field;
        }

        if (field === 'feed_total') {
          obj.hidden = false;
          obj.canUpdate = false;
        } else {
          obj.canUpdate = true;
        }

        materialTypeConfig.push(obj);
        materialTypeObj[field] = obj
      }
      self.materialTypeConfig = materialTypeConfig;
      self.materialTypeObj = materialTypeObj;
    }
  };

  this.checkValid = function(){
    var materialTypeConfig = this.materialTypeConfig;
    for (var i = 0; i < materialTypeConfig.length; i++) {
      var config = materialTypeConfig[i];
      var width = config.width;
      if (width === undefined) {
        return config.field;
      }
    }
    return '';
  };

  this.getObject = function(){
    var materialTypeConfig = this.materialTypeConfig;
    var templateConfig = {};
    for (var i = 0; i < materialTypeConfig.length; i++) {
      var config = materialTypeConfig[i];
      var obj = {
        width:config.width,
        align:config.align,
        hidden:config.hidden
      };
      templateConfig[config.field] = obj;
    }

    return templateConfig;
  }

  fn.initData(templates);
};
